package com.zxs.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zxs.common.entity.Result;
import com.zxs.domain.convert.SubjectLabelConvert;
import com.zxs.domain.entity.bo.SubjectLabelBO;
import com.zxs.domain.entity.dto.SubjectLabelDTO;
import com.zxs.domain.service.SubjectLabelDomainService;
import com.zxs.infra.basic.entity.SubjectMapping;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 标签模块
 *
 * @author zxs
 * @date 2025/11/9 下午2:50
 */
@RestController
@RequestMapping("/label")
@Slf4j
public class labelController {

    @Autowired
    private SubjectLabelDomainService subjectLabelDomainService;

    /**
     * 新增标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/addLabel")
    public Result<Boolean> insertCategory(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        log.info("新增标签传递的参数-----{}", JSON.toJSONString(subjectLabelDTO));
        Result<Boolean> result = new Result<>();
        try {
            //参数校验
            Preconditions.checkNotNull(!StringUtils.isBlank(subjectLabelDTO.getLableName()), "标签名称不能为空！");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvert.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean aBoolean = subjectLabelDomainService.addSubjectLabel(subjectLabelBO);
            if (aBoolean) {
                result = Result.success("新增题目标签成功！", aBoolean);
            }
        } catch (Exception e) {
            log.error("新增标签，报错{}", e.getMessage());
            result = Result.failed("服务器异常，请联系管理员！");
            return result;
        }
        return result;
    }

    /**
     * 新增标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> updateCategory(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        log.info("更新标签传递的参数-----{}", JSON.toJSONString(subjectLabelDTO));
        Result<Boolean> result = new Result<>();
        try {
            //参数校验
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空！");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvert.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean aBoolean = subjectLabelDomainService.updateSubjectLabel(subjectLabelBO);
            if (aBoolean) {
                result = Result.success("修改题目标签成功！", aBoolean);
                return result;
            }
            result = Result.failed("修改标签失败");
        } catch (Exception e) {
            log.error("修改标签，报错{}", e.getMessage());
            result = Result.failed("服务器异常，请联系管理员！");
            return result;
        }
        return result;
    }


    /**
     * 删除标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> deleteCategory(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        log.info("删除标签传递的参数-----{}", JSON.toJSONString(subjectLabelDTO));
        Result<Boolean> result = new Result<>();
        try {
            //参数校验
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不能为空！");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvert.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            Boolean aBoolean = subjectLabelDomainService.deleteSubjectLabel(subjectLabelBO);
            if (aBoolean) {
                result = Result.success("删除题目标签成功！", aBoolean);
                return result;
            }
            result = Result.failed("删除标签失败");
        } catch (Exception e) {
            log.error("修改标签，报错{}", e.getMessage());
            result = Result.failed("服务器异常，请联系管理员！");
            return result;
        }
        return result;
    }

    /**
     * 根据分类id查询标签
     * @param subjectLabelDTO
     * @return
     */
    @PostMapping("/queryByCategoryId")
    public Result<List<SubjectLabelBO>> queryByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        log.info("根据分类id查询标签传递的参数-----{}", JSON.toJSONString(subjectLabelDTO));
        Result<List<SubjectLabelBO>> result = new Result<>();
        try {
            //参数校验
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不能为空！");
            SubjectLabelBO subjectLabelBO = SubjectLabelConvert.INSTANCE.convertDtoToLabelBO(subjectLabelDTO);
            List<SubjectMapping> subjectLabelDTOList = subjectLabelDomainService.queryByCategoryId(subjectLabelBO);
            result = Result.success("根据分类id查询标签成功！", subjectLabelDTOList);
            return result;
        } catch (Exception e) {
            log.error("根据分类id查询标签，报错{}", e.getMessage());
            result = Result.failed("服务器异常，请联系管理员！");
            return result;
        }
    }


}